package com.us.usws.support.uitls;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.us.usws.common.SiteConstants;
import com.us.usws.model.flat.system.DOUser;

/**
 * @author Wang Jianbo
 */
public abstract class WebContext {

	private HttpServletRequest request;
	private HttpServletResponse response;

	/**
	 * constructor
	 * @param request HttpServletRequest instance
	 * @param response HttpServletResponse instance
	 */
	public WebContext(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	/**
	 * @return the request
	 */
	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * @return the response
	 */
	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * get session
	 * @return
	 */
	public HttpSession getSession() {
		return request.getSession(true);
	}

	/**
	 * get logged-in user
	 * @return
	 */
	public DOUser getUser() {
		return (DOUser) this.request.getSession().getAttribute(SiteConstants.SESSION_KEY_LOGIN_USER);
	}

	/**
	 * set user to session
	 * @param user
	 */
	public void setUser(DOUser user) {
		this.request.getSession().setAttribute(SiteConstants.SESSION_KEY_LOGIN_USER, user);
	}

	/**
	 * whether is logged in
	 * @return
	 */
	public boolean isLogin() {
		return getUser() == null ? false : true;
	}

}
